package com.twu.biblioteca;

import java.util.*;

/**
 * Created by kathy on 12/04/14.
 * This method demonstrates action of listing available books
 */
public class ListBooksAction implements Action {

    private Library library;

    public ListBooksAction(Library library){
        this.library = library;
    }

    public void execute(){
        List<Book> availableBooks = listAvailableBooks();
        printBooks(availableBooks);
    }

    // This method is to print books in a clear format
    private void printBooks(List<Book> bookList) {

        System.out.print("ID \t\t\t");
        System.out.print("Name \t\t\t");
        System.out.print("Author \t\t\t");
        System.out.print("Edition \t\t\n");

        for(Book abook : bookList){
            abook.printBook();
        }
    }

    // This method is to get all books which are ready to checkout
    public List<Book> listAvailableBooks(){

        List<Book> availableBooks = new ArrayList<Book>();

        for(Book book : library.getBookRepository()){
            if(book.getUser() == null)
                availableBooks.add(book);
        }

        return availableBooks;
    }


}
